//
//  AFBHDConverter.m
//  TCPSocket_server
//
//  Created by liuquan on 2018/5/16.
//  Copyright © 2018年 liuquan. All rights reserved.
//

#import "ASFHConverter.h"
#import <ifaddrs.h>
#import <arpa/inet.h>

@implementation ASFHConverter

// 十六进制转十进制
+ (NSString *)convert_10_with_16:(NSString  *)hexString{
    
    // 十六进制转二进制
    NSString *tempStr = [self convert_2_with_16:hexString];
    // 二进制转十进制
    return [NSString stringWithFormat:@"%ld",[self getDecimalByBinary:tempStr]];
}

// 十进制字符串转十六进制
+ (NSString *)convert_16_with_10:(NSString *)str{
    NSString *tempBit;
    if (str.intValue < 16) {
        tempBit = [NSString stringWithFormat:@"0%@",[[NSString alloc] initWithFormat:@"%1x",[str intValue]]];//[NSString stringWithFormat:@"0%@",str];
    }else{
        tempBit = [NSString stringWithFormat:@"%@",[[NSString alloc] initWithFormat:@"%1x",[str intValue]]];
    }
    return tempBit;
}


// 普通字符串转换为十六进制的。
+ (NSString *)convert_16_with_commonString:(NSString *)string{
    NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding];
    Byte *bytes = (Byte *)[myD bytes];
    //下面是Byte 转换为16进制。
    NSString *hexStr=@"";
    for(int i=0;i<[myD length];i++)
        
    {
        NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数
        
        if([newHexStr length]==1)
            
            hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
        
        else
            
            hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
    }
    return hexStr;
}


// 十六进制字符串转十六进制data
+ (NSData *)convert_16Data_with_16String:(NSString *)str {
    if (!str || [str length] == 0) {
        return nil;
    }
    
    NSMutableData *hexData = [[NSMutableData alloc] initWithCapacity:8];
    NSRange range;
    if ([str length] % 2 == 0) {
        range = NSMakeRange(0, 2);
    } else {
        range = NSMakeRange(0, 1);
    }
    for (NSInteger i = range.location; i < [str length]; i += 2) {
        unsigned int anInt;
        NSString *hexCharStr = [str substringWithRange:range];
        NSScanner *scanner = [[NSScanner alloc] initWithString:hexCharStr];
        
        [scanner scanHexInt:&anInt];
        NSData *entity = [[NSData alloc] initWithBytes:&anInt length:1];
        [hexData appendData:entity];
        
        range.location += range.length;
        range.length = 2;
    }
    
    return hexData;
}

// 十六进制data转十六进制字符串
+ (NSString *)convert_16String_with_16Data:(NSData *)data {
    if (!data || [data length] == 0) {
        return @"";
    }
    NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]];
    
    [data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop) {
        unsigned char *dataBytes = (unsigned char*)bytes;
        for (NSInteger i = 0; i < byteRange.length; i++) {
            NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) & 0xff];
            if ([hexStr length] == 2) {
                [string appendString:[NSString stringWithFormat:@"%@ ",hexStr]];
            } else {
                [string appendFormat:@"0%@ ", hexStr];
            }
        }
    }];
    
    return string;
}

// 获取设备的IP
+ (NSString *)deviceIPAdress {
    NSString *address = @"an error occurred when obtaining ip address";
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    int success = 0;
    
    success = getifaddrs(&interfaces);
    
    if (success == 0) { // 0 表示获取成功
        
        temp_addr = interfaces;
        while (temp_addr != NULL) {
            if( temp_addr->ifa_addr->sa_family == AF_INET) {
                // Check if interface is en0 which is the wifi connection on the iPhone
                if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
                    // Get NSString from C String
                    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
                }
            }
            
            temp_addr = temp_addr->ifa_next;
        }
    }
    
    freeifaddrs(interfaces);
    return address;
}

// 获取当前的时间
+ (NSString *)getCurrentTime{
    
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"HH:mm:ss";
    NSString *currentDate = [fmt stringFromDate:[NSDate date]];
    return currentDate;
}

// 16进制转10进制

+ (NSNumber *) numberHexString:(NSString *)aHexString

{
    
    // 为空,直接返回.
    
    if (nil == aHexString)
        
    {
        
        return nil;
        
    }
    
    
    
    NSScanner * scanner = [NSScanner scannerWithString:aHexString];
    
    unsigned long long longlongValue;
    
    [scanner scanHexLongLong:&longlongValue];
    
    
    
    //将整数转换为NSNumber,存储到数组中,并返回.
    
    NSNumber * hexNumber = [NSNumber numberWithLongLong:longlongValue];
    
    
    
    return hexNumber;
    
}

/**
 十六进制转换为二进制
 
 @param hex 十六进制数
 @return 二进制数
 */
+ (NSString *)convert_2_with_16:(NSString *)hex {
    
    NSMutableDictionary *hexDic = [[NSMutableDictionary alloc] initWithCapacity:16];
    [hexDic setObject:@"0000" forKey:@"0"];
    [hexDic setObject:@"0001" forKey:@"1"];
    [hexDic setObject:@"0010" forKey:@"2"];
    [hexDic setObject:@"0011" forKey:@"3"];
    [hexDic setObject:@"0100" forKey:@"4"];
    [hexDic setObject:@"0101" forKey:@"5"];
    [hexDic setObject:@"0110" forKey:@"6"];
    [hexDic setObject:@"0111" forKey:@"7"];
    [hexDic setObject:@"1000" forKey:@"8"];
    [hexDic setObject:@"1001" forKey:@"9"];
    [hexDic setObject:@"1010" forKey:@"A"];
    [hexDic setObject:@"1011" forKey:@"B"];
    [hexDic setObject:@"1100" forKey:@"C"];
    [hexDic setObject:@"1101" forKey:@"D"];
    [hexDic setObject:@"1110" forKey:@"E"];
    [hexDic setObject:@"1111" forKey:@"F"];
    
    NSString *binary = @"";
    for (int i=0; i<[hex length]; i++) {
        
        NSString *key = [hex substringWithRange:NSMakeRange(i, 1)];
        NSString *value = [hexDic objectForKey:key.uppercaseString];
        if (value) {
            
            binary = [binary stringByAppendingString:value];
        }
    }
    return binary;
}

/**
 二进制转换为十进制
 
 @param binary 二进制数
 @return 十进制数
 */
+ (NSInteger)getDecimalByBinary:(NSString *)binary {
    
    NSInteger decimal = 0;
    for (int i=0; i<binary.length; i++) {
        
        NSString *number = [binary substringWithRange:NSMakeRange(binary.length - i - 1, 1)];
        if ([number isEqualToString:@"1"]) {
            
            decimal += pow(2, i);
        }
    }
    return decimal;
}

// 时间转换
+ (NSString *)hexTimeWithTime:(NSString *)time{
    
    NSArray *timeArr = [time componentsSeparatedByString:@":"];
    NSMutableString *timeStr = [NSMutableString string];
    for (NSString *time in timeArr) {
        NSString *timeHex = [self convert_16_with_10:time];
        [timeStr appendString:timeHex];
    }
    return timeStr;
}

// IP转换  十进制转16进制
+ (NSString *)hexIPWithIP:(NSString *)ip{
    
    NSArray *ipArr = [ip componentsSeparatedByString:@"."];
    NSMutableString *ipStr = [NSMutableString string];
    for (NSString *tempIP in ipArr) {
        NSString *ipHex = [self convert_16_with_10:tempIP];
        [ipStr appendString:ipHex];
    }
    return ipStr;
}

//////定时器

+ (void)lq_pauseTimer:(NSTimer *)timer{

    if (![timer isValid]) {
        return ;
    }
    [timer setFireDate:[NSDate distantFuture]];
}


+ (void)lq_resumeTimer:(NSTimer *)timer{
    if (![timer isValid]) {
        return ;
    }
    [timer setFireDate:[NSDate date]];
}


+ (void)lq_destoryTimer:(NSTimer *)timer{
    
    if ([timer isValid]) {
        [timer invalidate];
    }
}


@end
